home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_tutor.arc / TEXT.ARC / CHAP4.TXT < prev    next >
Text File  |  1990-08-08  |  33KB  |  718 lines

  1.  
  2.                  Chapter 4 - Assignment & Logical compares
  3.  
  4.  
  5.                        INTEGER ASSIGNMENT STATEMENTS
  6.  
  7.              Load the file INTASIGN.C and display it for an  example
  8.         of  assignment statements.   Three variables are defined for
  9.         use  in the program and the rest of the program is merely  a
  10.         series of illustrations of various assignments.   The  first
  11.         two  lines  of  the assignment statements  assign  numerical
  12.         values  to "a" and "b",  and the next five lines  illustrate
  13.         the  five  basic arithmetic functions and how to  use  them.
  14.         The  fifth is the modulo operator and gives the remainder if
  15.         the two variables were divided.   It can only be applied  to
  16.         "int"  or  "char"  type  variables,   and  of  course  "int"
  17.         extensions such as "long",  "short",  etc.  Following these,
  18.         there  are two lines illustrating how to combine some of the
  19.         variables  in  some complex math expressions.   All  of  the
  20.         above examples should require no comment except to say  that
  21.         none  of  the equations are meant to be particularly  useful
  22.         except  as illustrations.  The "char" type variable will  be
  23.         defined in the description of the next example program.
  24.  
  25.              The  expressions  in  lines 17  and  18  are  perfectly
  26.         acceptable  as given, but we will see later in this  chapter
  27.         that  there is another way to write these for  more  compact
  28.         code.
  29.  
  30.              This leaves us with the last two lines which may appear
  31.         to  you  as being very strange.   The C compiler  scans  the
  32.         assignment  statement from right to left,  (which may seem a
  33.         bit odd since we do not read that way),  resulting in a very
  34.         useful construct,  namely the one given here.   The compiler
  35.         finds the value 20, assigns it to "c", then continues to the
  36.         left finding that the latest result of a calculation  should
  37.         be  assigned to "b".   Thinking that the latest  calculation
  38.         resulted in a 20,  it assigns it to "b" also,  and continues
  39.         the leftward scan assigning the value 20 to "a" also.   This
  40.         is a very useful construct when you are initializing a group
  41.         of  variables.   The last statement illustrates that  it  is
  42.         possible  to actually do some calculations to arrive at  the
  43.         value  which  will be assigned to all three  variables.   In
  44.         fact,  the rightmost expression can contain variables,  even
  45.         "a", "b", & "c".
  46.  
  47.              The  program has no output, so compiling and  executing
  48.         this  program  will be very uninteresting.  Since  you  have
  49.         already  learned how to display some integer  results  using
  50.         the "printf" function, it would be to your advantage to  add
  51.         some output statements to this program to see if the various
  52.         statements do what you think they should do.
  53.  
  54.              This would be a good time for a preliminary  definition
  55.         of  a  rule to be followed in C.   The data definitions  are
  56.  
  57.  
  58.                                   Page 20
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                  Chapter 4 - Assignment & Logical compares
  69.  
  70.  
  71.         always given before any executable statements in any program
  72.         block.   This is why the variables are defined first in this
  73.         program and in every C program.  If you try to define a  new
  74.         variable  after  executing  some  statements,  the  Turbo  C
  75.         compiler, and every other C compiler will issue an error.
  76.  
  77.                            ADDITIONAL DATA TYPES
  78.  
  79.              Loading and editing MORTYPES.C will illustrate how some
  80.         additional  data  types can be used.   Once  again  we  have
  81.         defined  a  few integer type variables which you  should  be
  82.         fairly  familiar  with  by now,  but we have added  two  new
  83.         types, the "char", and the "float".
  84.  
  85.              The  "char"  type  of data is nearly the  same  as  the
  86.         integer  except that it can only be assigned values  between
  87.         -128 to 127, since it is stored in only one byte of  memory.
  88.         The "char" type of data is usually used for ASCII data, more
  89.         commonly  known  as  text.  The text  you  are  reading  was
  90.         originally written on a computer with a word processor  that
  91.         stored the words in the computer one character per byte.  In
  92.         contrast,  the integer data type is stored in two  bytes  of
  93.         computer memory on microcomputers using Turbo C.
  94.  
  95.                               DATA TYPE MIXING
  96.  
  97.              It  would be profitable at this time to discuss the way
  98.         C handles the two types "char" and "int".  Most functions in
  99.         C  that are designed to operate with integer type  variables
  100.         will work equally well with character type variables because
  101.         they  are a form of an integer variable.   Those  functions,
  102.         when called on to use a "char" type variable,  will actually
  103.         promote  the "char" data into integer data before using  it.
  104.         For this reason, it is possible to mix "char" and "int" type
  105.         variables in nearly any way you desire.   The compiler  will
  106.         not get confused,  but you might.  It is good not to rely on
  107.         this too much, but to carefully use only the proper types of
  108.         data where they should be used.
  109.  
  110.               The second new data type is the "float" type of  data,
  111.         commonly  called floating point data.  This is a  data  type
  112.         which  usually  has a very large range, a  large  number  of
  113.         significant digits, and a large number of computer words are
  114.         required  to store it.  The "float" data type has a  decimal
  115.         point  associated with it and, in Turbo C, has an  allowable
  116.         range of from 3.4E-38 to 3.4E+38, and is composed of about 7
  117.         significant digits.
  118.  
  119.                        HOW TO USE THE NEW DATA TYPES
  120.  
  121.              The  first three lines of the program assign values  to
  122.  
  123.  
  124.                                   Page 21
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                  Chapter 4 - Assignment & Logical compares
  135.  
  136.  
  137.         all nine of the defined variables so we can manipulate  some
  138.         of the data between the different types.
  139.  
  140.              Since,  as  mentioned above,  a "char" data type is  in
  141.         reality  an "integer" data type,  no special  considerations
  142.         need be taken to promote a "char" to an "int",  and a "char"
  143.         type data field can be assigned to an "int" variable.   When
  144.         going the other way, there is no standard, so you may simply
  145.         get garbage if the value of the integer variable is  outside
  146.         the  range of the "char" type variable.   It will  translate
  147.         correctly if the value is within the range of -128 to 127.
  148.  
  149.              The   third   line   illustrates  the   simplicity   of
  150.         translating an integer into a "float",  simply assign it the
  151.         new  value  and the system will do  the  proper  conversion.
  152.         When  going  the  other  way  however,  there  is  an  added
  153.         complication.   Since  there may be a fractional part of the
  154.         floating  point number,  the system must decide what  to  do
  155.         with it.  By definitions , it will truncate it.
  156.  
  157.              This program produces no output, and we haven't covered
  158.         a way to print out "char" and "float" type variables, so you
  159.         can't  really  get  in  to this program and  play  with  the
  160.         results, but the next program will cover this for you.
  161.  
  162.              Compile  and  run  this program, then go  back  to  the
  163.         Edit/Message  screen (by pressing any key) and  observe  the
  164.         message  window.   Notice  that there  are  several  warning
  165.         messages telling you that 6 variables were assigned a  value
  166.         and  never  used.   These  warning  messages  are  for  your
  167.         benefit,  so  it would pay you to keep a close  eye  on  the
  168.         message window when writing and debugging a program.
  169.  
  170.                           LOTS OF VARIABLE TYPES
  171.  
  172.              Load the file LOTTYPES.C and display it on your screen.
  173.         This  file contains nearly every standard simple  data  type
  174.         available  in the programming language C.  There  are  other
  175.         types,  but  they are the compound types (ie  -  arrays  and
  176.         structures) that we will cover in due time.
  177.  
  178.              Observe  the  file.  First we define  a  simple  "int",
  179.         followed by a "long int" which has a range of -2147483648 to
  180.         2147483647  in Turbo C, and a "short int" which has a  range
  181.         that is identical to that for the "int" variable in Turbo C,
  182.         namely  -32768  to  32767. The "unsigned"  is  next  and  is
  183.         defined as the same size as the "int" but with no sign.  The
  184.         "unsigned" then will cover a range of 0 to 65535 in Turbo C.
  185.         It  should be pointed out that when the "long", "short",  or
  186.         "unsigned" is desired, the "int" is optional and is left out
  187.         by  most experienced programmers.  We have  already  covered
  188.  
  189.  
  190.                                   Page 22
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                  Chapter 4 - Assignment & Logical compares
  201.  
  202.  
  203.         the "char" and the "float", which leaves only the  "double".
  204.         The "double" covers a greater range than the "float" and has
  205.         more  significant digits for more precise calculations.   It
  206.         also  requires more memory to store a value than the  simple
  207.         "float". The "double" in Turbo C covers a range of  1.7E-308
  208.         to 1.7E+308.
  209.  
  210.              Note  that other compounding of types can be done  such
  211.         as "long unsigned int", "unsigned char", etc.  See pages  84
  212.         to 86 of the TURBO C version 1.0 User's Guide.
  213.  
  214.              Another  diversion  is in order at  this  point.   Your
  215.         Turbo  C  compiler,  like  most other  C  compilers  has  no
  216.         provisions for floating point math, but only double floating
  217.         point math.  It will promote a "float" to a "double"  before
  218.         doing calculations and therefore only one math library  will
  219.         be  needed.  Of course, this is totally transparent to  you,
  220.         so  you don't need to worry about it.  Because of this,  you
  221.         may  think  that  it would be best to  simply  define  every
  222.         floating  point variable as double, since they are  promoted
  223.         before  use in any calculations, but that may not be a  good
  224.         idea.  A "float" variable requires 4 bytes of storage and  a
  225.         "double" requires 8 bytes of storage, so if you have a large
  226.         volume  of floating point data to store, the  "double"  will
  227.         obviously require much more memory.
  228.  
  229.              After  defining  the data types in  the  program  under
  230.         consideration, a numerical value is assigned to each of  the
  231.         defined  variables  in  order to demonstrate  the  means  of
  232.         outputting each to the monitor.
  233.  
  234.                             THE CONVERSION CHARACTERS
  235.  
  236.              Following   is  a  list  of  some  of  the   conversion
  237.         characters  and  the  way  they are  used  in  the  "printf"
  238.         statement.   A  complete  list  of  all  of  the  conversion
  239.         characters  is  given  on page 171 to 178  of  the  Turbo  C
  240.         Reference Guide.
  241.  
  242.              d    decimal notation
  243.              o    octal notation
  244.              x    hexadecimal notation
  245.              u    unsigned notation
  246.              c    character notation
  247.              s    string notation
  248.              f    floating point notation
  249.  
  250.              Each  of  these  is used following a  percent  sign  to
  251.         indicate  the type of output conversion,  and between  those
  252.         two characters, the following  fields may be added.
  253.  
  254.  
  255.  
  256.                                   Page 23
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                  Chapter 4 - Assignment & Logical compares
  267.  
  268.  
  269.              -    left justification in its field
  270.              (n)  a number specifying minimum field width
  271.              .    to separate n from m
  272.              (m)  significant fractional digits for a float
  273.              l    to indicate a "long"
  274.  
  275.              These  are all used in the examples which are  included
  276.         in the program presently displayed on your monitor, with the
  277.         exception of the string notation which will be covered later
  278.         in  this tutorial. Note especially the variable field  width
  279.         specification  demonstrated in lines 33 to 36.  This is  not
  280.         part of the original definition of C, but it is included  in
  281.         the  proposed  ANSI standard and will become part of  the  C
  282.         language.   Compile and run this program to see what  effect
  283.         the various fields have on the output.
  284.  
  285.              You  now  have the ability to display any of  the  data
  286.         fields  in  the  previous programs and it would be  to  your
  287.         advantage to go back and see if you can display some of  the
  288.         fields anyway you desire.
  289.  
  290.                               LOGICAL COMPARES
  291.  
  292.              Load  and  view  the file  named  COMPARES.C  for  many
  293.         examples  of compare statements in C.   We begin by defining
  294.         and  initializing  nine variables to use  in  the  following
  295.         compare  statements.   This initialization is new to you and
  296.         can be used to initialize variables while they are defined.
  297.  
  298.              The  first  group of compare statements represents  the
  299.         simplest  kinds  of compares since they simply  compare  two
  300.         variables.    Either  variable  could  be  replaced  with  a
  301.         constant and still be a valid compare,  but two variables is
  302.         the general case.  The first compare checks to see if "x" is
  303.         equal  to  "y"  and it uses the double equal  sign  for  the
  304.         comparison.   A single equal sign could be used here but  it
  305.         would have a different meaning as we will see shortly.   The
  306.         second  comparison checks to see if "x" is greater than "z".
  307.  
  308.              The  third compare introduces the "NOT"  operator,  the
  309.         exclamation,  which can be used to invert the result of  any
  310.         logical  compare.   The fourth checks for "b" less  than  or
  311.         equal to "c", and the last checks for "r" not equal to  "s".
  312.         As  we  learned in the last chapter, if the  result  of  the
  313.         compare  is  true, the statement following the  "if"  clause
  314.         will be executed and the results are given in the  comments.
  315.         Note  that  "less than" and "greater than or equal  to"  are
  316.         also available, but are not illustrated here.
  317.  
  318.              It  would be well to mention the different format  used
  319.         for the "if" statement in this example program.   A carriage
  320.  
  321.  
  322.                                   Page 24
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.                  Chapter 4 - Assignment & Logical compares
  333.  
  334.  
  335.         return  is  not  required as a statement  separator  and  by
  336.         putting the conditional clause on the same line as the "if",
  337.         it adds to the readability of the overall program.
  338.  
  339.                                MORE COMPARES
  340.  
  341.              The  compares  in  the  second group  are  a  bit  more
  342.         involved.  Starting with the first compare, we find a rather
  343.         strange  looking set of conditions in the  parentheses.   To
  344.         understand  this  we must understand just what a  "true"  or
  345.         "false"  is in the C language.   A "false" is defined  as  a
  346.         value  of zero,  and "true" is defined as a non-zero  value.
  347.         Any  integer  or char type of variable can be used  for  the
  348.         result of a true/false test, or the result can be an implied
  349.         integer or char.
  350.  
  351.              Look  at  the  first  compare of the  second  group  of
  352.         compare statements.   The expression "r != s" will  evaluate
  353.         as  a "true" since "r" was set to 0.0 above,  so the  result
  354.         will be a non-zero value.  In Turbo C, it seems to always be
  355.         a  1,  but you could get in trouble if you wrote  a  program
  356.         that depended on it being 1 in all cases.  Good  programming
  357.         practice  would  be  to  not use  the  resulting  1  in  any
  358.         calculations.   Even  though  the  two  variables  that  are
  359.         compared  are "float" variables, the result will be of  type
  360.         "integer".   There is no explicit variable to which it  will
  361.         be  assigned  so  the result of the compare  is  an  implied
  362.         integer.   Finally the resulting number, probably 1 in  this
  363.         case,  is assigned to the integer variable "x".   If  double
  364.         equal signs were used, the phantom value, namely 1, would be
  365.         compared  to  the value of "x", but since the  single  equal
  366.         sign  is  used, the value 1 is simply assigned  to  "x",  as
  367.         though  the  statement were not  in  parentheses.   Finally,
  368.         since  the result of the assignment in the  parentheses  was
  369.         non-zero, the entire expression is evaluated as "true",  and
  370.         "z" is assigned the value of 1000.  Thus we accomplished two
  371.         things  in  this  statement, we assigned "x"  a  new  value,
  372.         probably  1,  and  we assigned "z" the value  of  1000.   We
  373.         covered a lot in this statement so you may wish to review it
  374.         before  going on.  The important things to remember are  the
  375.         values  that  define "true" and "false", and the  fact  that
  376.         several  things can be assigned in a conditional  statement.
  377.         The  value  assigned to "x" was probably a 1,  but  remember
  378.         that the only requirement is that it is nonzero.
  379.  
  380.              The next example should help clear up some of the above
  381.         in your mind.  In this example, "x" is assigned the value of
  382.         "y",  and since the result is 11, the condition is non-zero,
  383.         which is true,   and the variable "z" is therefore  assigned
  384.         222.
  385.  
  386.  
  387.  
  388.                                   Page 25
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.                  Chapter 4 - Assignment & Logical compares
  399.  
  400.  
  401.              The third example, in the second group, compares "x" to
  402.         zero.   If  the result is true,  meaning that if "x" is  not
  403.         zero,  then "z" is assigned the value of 333,  which it will
  404.         be.   The  last  example in this group illustrates the  same
  405.         concept,  since the result will be true if "x" is  non-zero.
  406.         The compare to zero is not actually needed and the result of
  407.         the compare is true.   The third and fourth examples of this
  408.         group are therefore identical.
  409.  
  410.                         ADDITIONAL COMPARE CONCEPTS
  411.  
  412.              The   third  group  of  compares  will  introduce  some
  413.         additional  concepts,  namely  the  logical  "AND"  and  the
  414.         logical  "OR".   We  assign  the value of 77  to  the  three
  415.         integer  variables  simply to get started  again  with  some
  416.         defined  values.   The  first  compare of  the  third  group
  417.         contains  the new control "&&",  which is the logical "AND".
  418.         The  entire statement reads,  if "x" equals "y" AND  if  "x"
  419.         equals  77 then the result is "true".   Since this is  true,
  420.         the variable z is set equal to 33.
  421.  
  422.              The  next  compare  in this group introduces  the  "||"
  423.         operator which is the "OR".   The statement reads, if "x" is
  424.         greater  than  "y"  OR if "z" is greater than  12  then  the
  425.         result is true.   Since "z" is greater than 12,  it  doesn't
  426.         matter  if "x" is greater than "y" or not,  because only one
  427.         of  the  two conditions must be true for the  result  to  be
  428.         true.  The result is true, so therefore "z" will be assigned
  429.         the value of 22.
  430.  
  431.                              LOGICAL EVALUATION
  432.  
  433.              When a compound expression is evaluated, the evaluation
  434.         proceeds from left to right and as soon as the result of the
  435.         outcome is assured,  evaluation stops.   Namely, in the case
  436.         of  an "AND" evaluation,  when one of the terms evaluates to
  437.         "false",  evaluation is discontinued because additional true
  438.         terms  cannot make the result ever become  "true".   In  the
  439.         case of an "OR" evaluation,  if any of the terms is found to
  440.         be  "true",  evaluation stops because it will be  impossible
  441.         for additional terms to cause the result to be "false".   In
  442.         the case of additionally nested terms,  the above rules will
  443.         be applied to each of the nested levels.
  444.  
  445.                           PRECEDENCE OF OPERATORS
  446.  
  447.              The  question will come up concerning the precedence of
  448.         operators.   Which  operators are evaluated first and  which
  449.         last?   There are many rules about this topic, but  I  would
  450.         suggest  that  you  don't  worry about  it  at  this  point.
  451.         Instead,  use  lots  of  parentheses  to  group   variables,
  452.  
  453.  
  454.                                   Page 26
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.                  Chapter 4 - Assignment & Logical compares
  465.  
  466.  
  467.         constants,  and  operators  in  a  way  meaningful  to  you.
  468.         Parentheses always have the highest priority and will remove
  469.         any  question of which operations will be done first in  any
  470.         particular statements.
  471.  
  472.              Going  on to the next example in group three,  we  find
  473.         three  simple variables used in the conditional part of  the
  474.         compare.   Since  all  three  are non-zero,  all  three  are
  475.         "true",  and therefore the "AND" of the three variables  are
  476.         true,  leading  to  the result being "true",  and "z"  being
  477.         assigned  the value of 11.   Note that since the  variables,
  478.         "r", "s", and "t" are "float" type variables, they could not
  479.         be  used this way,  but they could each be compared to  zero
  480.         and the same type of expression could be used.
  481.  
  482.              Continuing on to the fourth example of the third  group
  483.         we  find three assignment statements in the compare part  of
  484.         the "if" statement.  If you understood the above discussion,
  485.         you  should have no difficulty understanding that the  three
  486.         variables are assigned their respective new values,  and the
  487.         result  of  all three are non-zero,  leading to a  resulting
  488.         value of "TRUE".
  489.  
  490.                         THIS IS A TRICK, BE CAREFUL
  491.  
  492.              The last example of the third group contains a bit of a
  493.         trick, but since we have covered it above, it is nothing new
  494.         to you.  Notice that the first part of the compare evaluates
  495.         to  "FALSE".   The  remaining parts of the compare  are  not
  496.         evaluated,  because it is an "AND" and it will definitely be
  497.         resolved as a "FALSE" because the first term is  false.   If
  498.         the program was dependent on the value of "y" being set to 3
  499.         in  the  next  part of the compare,  it  will  fail  because
  500.         evaluation  will  cease following the "FALSE" found  in  the
  501.         first  term.   Likewise,  "z" will not be set to 4,  and the
  502.         variable "r" will not be changed.
  503.  
  504.                           POTENTIAL PROBLEM AREAS
  505.  
  506.              The   last   group   of   compares   illustrate   three
  507.         possibilities for getting into a bit of trouble.   All three
  508.         have  the  common  result that "z" will not get set  to  the
  509.         desired value,  but for different reasons.   In the case  of
  510.         the  first  one,  the compare evaluates as "true",  but  the
  511.         semicolon  following the second parentheses  terminates  the
  512.         "if"  clause,  and the assignment statement involving "z" is
  513.         always executed as the next statement.   The "if"  therefore
  514.         has  no  effect  because of the  misplaced  semicolon.   The
  515.         second  statement is much more straightforward  because  "x"
  516.         will  always  be equal to itself,  therefore the  inequality
  517.         will never be true, and the entire statement will never do a
  518.  
  519.  
  520.                                   Page 27
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.                  Chapter 4 - Assignment & Logical compares
  531.  
  532.  
  533.         thing, but is wasted effort.  The last statement will always
  534.         assign  0  to "x" and the compare will therefore  always  be
  535.         "false",  never  executing the conditional part of the  "if"
  536.         statement.
  537.  
  538.              The  conditional  statement is extremely important  and
  539.         must be thoroughly understood to write efficient C programs.
  540.         If  any  part of this discussion is unclear  in  your  mind,
  541.         restudy  it  until you are confident that you understand  it
  542.         thoroughly before proceeding onward.
  543.  
  544.              Compile and run this program and once again, return  to
  545.         the  Edit/Message  screen to observe the  warning  messages.
  546.         The Turbo C compiler is warning us that we have a  "possibly
  547.         incorrect assignment in function main" because we have  used
  548.         an  assignment statement within the conditional part of  the
  549.         "if" statements.  Once again, the warnings are for our  aid,
  550.         and  should  be carefully attended to.  In  this  case,  the
  551.         assignments   were  purposely  inserted  for   instructional
  552.         purposes  but assignments are not generally used in  compare
  553.         statements  and  Turbo C is warning you that  you  may  have
  554.         intended to use the double equal sign.
  555.  
  556.              Compile and run this program, then add some printout to
  557.         see the results of some of the comparisons.
  558.  
  559.                            THE CRYPTIC PART OF C
  560.  
  561.              There are three constructs used in C that make no sense
  562.         at   all  when  first  encountered  because  they  are   not
  563.         intuitive,  but they greatly increase the efficiency of  the
  564.         compiled  code  and  are used extensively by  experienced  C
  565.         programmers.   You  should therefore be exposed to them  and
  566.         learn to use them because they will appear in most,  if  not
  567.         all,  of the programs you see in the publications.  Load and
  568.         examine  the file named CRYPTIC.C for examples of the  three
  569.         new constructs.
  570.  
  571.              In  this  program,   some  variables  are  defined  and
  572.         initialized in the same statements for use below.  The first
  573.         executable statement simply adds 1 to the value of "x",  and
  574.         should come as no surprise to you.   The next two statements
  575.         also  add one to the value of "x",  but it is not  intuitive
  576.         that this is what happens.   It is simply by definition that
  577.         this is true.  Therefore, by definition of the C language, a
  578.         double   plus  sign  either  before  or  after  a   variable
  579.         increments  that variable by 1.   Additionally,  if the plus
  580.         signs are before the variable,  the variable is  incremented
  581.         before  it  is used,  and if the plus signs  are  after  the
  582.         variable,  the variable is used,  then incremented.   In the
  583.         next statement, the value of "y" is assigned to the variable
  584.  
  585.  
  586.                                   Page 28
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.                  Chapter 4 - Assignment & Logical compares
  597.  
  598.  
  599.         "z",  then  "y"  is incremented because the plus  signs  are
  600.         after  the  variable  "y".   In the last  statement  of  the
  601.         incrementing  group of example statements,  the value of "y"
  602.         is  incremented then its value is assigned to  the  variable
  603.         "z".
  604.  
  605.              The  next group of statements illustrate decrementing a
  606.         variable by one.   The definition works exactly the same way
  607.         for decrementing as it does for incrementing.   If the minus
  608.         signs are before the variable,  the variable is decremented,
  609.         then  used,  and if the minus signs are after the  variable,
  610.         the variable is used, then decremented.
  611.  
  612.                       THE CRYPTIC ARITHMETIC OPERATOR
  613.  
  614.              Another  useful but cryptic operator is the  arithmetic
  615.         operator.   This operator is used to modify any variable  by
  616.         some constant value.  The first statement of the "arithmetic
  617.         operator" group of statements simply adds 12 to the value of
  618.         the variable "a".   The second statement does the same,  but
  619.         once again, it is not intuitive that they are the same.  Any
  620.         of the four basic functions of arithmetic, "+", "-", "*", or
  621.         "/",  can  be handled in this way,  by putting the  function
  622.         desired  in  front  of the equal sign  and  eliminating  the
  623.         second  reference to the variable name.   It should be noted
  624.         that  the  expression on the right side  of  the  arithmetic
  625.         operator can be any valid expression,  the examples are kept
  626.         simple for your introduction to this new operator.
  627.  
  628.              Just  like the incrementing and decrementing operators,
  629.         the arithmetic operator is used extensively by experienced C
  630.         programmers and it would pay you well to understand it.
  631.  
  632.                          THE CONDITIONAL EXPRESSION
  633.  
  634.              The  conditional expression is just as cryptic  as  the
  635.         last  two,  but once again it can be very useful so it would
  636.         pay you to understand it.   It consists of three expressions
  637.         within parentheses separated by a question mark and a colon.
  638.         The  expression prior to the question mark is  evaluated  to
  639.         determine  if it is "true" or "false".   If it is true,  the
  640.         expression  between  the  question mark  and  the  colon  is
  641.         evaluated,  and if it is not true,  the expression following
  642.         the  colon  is evaluated.   The result of the evaluation  is
  643.         used for the assignment.   The final result is identical  to
  644.         that  of an "if" statement with an "else" clause.   This  is
  645.         illustrated  by  the  second example  in  this  group.   The
  646.         conditional  expression  has  the added  advantage  of  more
  647.         compact code that will compile to fewer machine instructions
  648.         in the final program.
  649.  
  650.  
  651.  
  652.                                   Page 29
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.                  Chapter 4 - Assignment & Logical compares
  663.  
  664.  
  665.              The  final two lines of this example program are  given
  666.         to  illustrate  a very compact way to assign the greater  of
  667.         two variables "a" or "b" to "c", and to assign the lessor of
  668.         the  same two variables to "c".   Notice how  efficient  the
  669.         code is in these two examples.
  670.  
  671.              Compile and run this program and notice that once again
  672.         we  get  a few warning to take heed to.  Since we  are  only
  673.         demonstrating how to do these things, they are not  actually
  674.         a problem but in a real program, these assignments would  be
  675.         wasted code.
  676.  
  677.                      TO BE CRYPTIC OR NOT TO BE CRYPTIC
  678.  
  679.              Several students of C have stated that they didn't like
  680.         these  three  cryptic constructs and that they would  simply
  681.         never  use  them.  This will be fine if they never  have  to
  682.         read  anybody  else's  program, or use  any  other  programs
  683.         within their own.  I have found many functions that I wished
  684.         to  use within a program but needed a small modification  to
  685.         use  it, requiring me to understand another  person's  code.
  686.         It  would therefore be to your advantage to learn these  new
  687.         constructs, and use them. They will be used in the remainder
  688.         of this tutorial, so you will be constantly exposed to them.
  689.  
  690.              This has been a long chapter but it contained important
  691.         material  to  get  you  started in using  C.   In  the  next
  692.         chapter,  we  will go on to the building blocks  of  C,  the
  693.         functions.  At that point, you will have enough of the basic
  694.         materials to allow you to begin writing meaningful programs.
  695.  
  696.         PROGRAMMING EXERCISES
  697.  
  698.         1.   Write  a program that will count from 1 to 12 and print
  699.              the count, and its square, for each count.
  700.                   1    1
  701.                   2    4
  702.                   3    9   etc.
  703.  
  704.         2.   Write a program that counts from 1 to 12 and prints the
  705.              count  and  its  inversion  to  5  decimal  places  for
  706.              each count. This will require a floating point number.
  707.                   1    1.00000
  708.                   2     .50000
  709.                   3     .33333
  710.                   4     .25000
  711.                   etc.
  712.  
  713.         3.   Write a program that will count from 1 to 100 and print
  714.              only those values between 32 and 39, one to a line.
  715.  
  716.  
  717.                                   Page 30
  718.